var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource from api_v1_auth get');
});

router.post('/', function (req, res) {
    console.log("IN REQUEST HANDLER");
    // console.log("url: " + req.url);
    // console.log("method: " + req.method);
    // console.log("headers: " + req.headers);
    // console.log("body: " + req.body);
    
    
    var pj, isValidUser;
    
    // req.body is a JSON object;
    pj = req.body;
    console.log("user_id is " + pj.user_id + " , password is " + pj.password);
    // console.log(pj.user_id);
    // console.log(pj.password);
    
    // Do authentication of user_id and password against database.
    isValidUser = false;
    // TODO: Add code to check credentials against database.
    
    // Assume that user credentials are valid
    isValidUser = true;
    
    if (isValidUser) {
        res.json({
            'result_code'   : 0,
            'result'        : 'Is valid user',
            'cred_code'     : 'PR'
        });
    } else {
        res.json({
            'result_code'   : -1,
            'result'        : 'Is invalid user',
            'cred_code'     : undefined
        });
    }
    
    // var json, pj;
    // json = req.body;
    // console.log("json: " + json.toString());
    // console.log("json: " + json.user_id);
    // console.log("json: " + req.body.user_id);
    // pj = JSON.parse(json.toString());
    // console.log("pj");
    // console.log(pj);
    
    // if (pj) {
    //     console.log(pj.length);
    //     console.log(pj["user_id"]);
    //     console.log(pj["password"]);
    // }
    // //console.log(req);
    // res.json({
    //    'foo' : 'bar' 
    // });
});

module.exports = router;

